import os
from books.shuquge_com import ShuqugeComSpider
from books.tangsanshu_la import TangsanshuLaSpider
from books.zongheng_com import ZonghengComSpider
from books.d1zw_com import D1zwComSpider


def shuquge_com():
    print('''------------------------------------------
1. 重返1989
0.自定义
------------------------------------------''')
    num = int(input('请输入要下载的小说序号：'))
    url = ''
    if (num == 1):
        url = 'https://www.ishuquge.com/txt/123110/index.html'
    else:
        url = input('请输入要爬取的书趣阁小说网址：\n')
    spider = ShuqugeComSpider(url)
    path = os.path.abspath('') + '/www.shuquge.com'
    spider.start(path)
    print(spider.__format__())
    print('小说爬取完成')


def tangsanshu_la():
    url = input('请输入要爬取的唐三书屋小说网址：\n')
    spider = TangsanshuLaSpider(url)
    path = os.path.abspath('') + '/www.tangsanshu.la'
    spider.start(path)
    print(spider.__format__())
    print('小说爬取完成')


def zongheng_com():
    url = input('请输入要爬取的纵横小说网址：\n')
    spider = ZonghengComSpider(url)
    path = os.path.abspath('') + '/book.zongheng.com'
    spider.start(path)
    print(spider.__format__())
    print('小说爬取完成')


def d1zw_com():
    url = input('请输入要爬取的第一中文网小说网址：\n')
    #url = 'http://www.d1zw.com/files/article/html/61/61045/'
    spider = D1zwComSpider(url)
    path = os.path.abspath('') + '/www.d1zw.com'
    spider.start(path)
    print(spider.__format__())
    print('小说爬取完成')


def main():
    while True:
        print('''==========================================
请输入要爬取的小说网站：
1.书趣阁:shuquge.com\t2.唐山书屋:tangsanshu.la\t3.纵横中文网:zhongheng.com\t4.第一中文网:d1zw.com
0.退出程序              
==========================================''')
        num = int(input('请输入要下载的小说网站序号：'))
        if num == 0:
            print('退出程序')
            break
        elif num == 1:
            shuquge_com()
        elif num == 2:
            tangsanshu_la()
        elif num == 3:
            zongheng_com()
        elif num == 4:
            d1zw_com()


if __name__ == '__main__':
    main()
    # try:
    #     main()
    # except Exception as e:
    #     print('\n程序异常退出: ', e)
